from Tkinter import *
from tkSimpleDialog import *
import re

class TextualAnalysisFrame(Frame):
    """
    Represents the frame that provides a user interface to access the lexical analysis of the project.
    """
    
    whitespaceRegex = re.compile(r"[\s\n\r]+")
    
    def __init__(self, parentControl, listener):
        Frame.__init__(self, parentControl)
        self.parentControl = parentControl
        self.listener = listener
        
        label = Label(parentControl, text="Mandat:")
        label.grid(column = 0, columnspan = 4)
        
        self.mandateTextBox = Text(parentControl, width=45, height=20)
        self.mandateTextBox.config(state = DISABLED)
        self.mandateTextBox.grid(row = 1, column = 0, columnspan = 4)
        
        label = Label(parentControl, text="Noms: ")
        label.grid(row = 2, column = 1)
        
        label = Label(parentControl, text="Verbes: ")
        label.grid(row = 2, column = 2)
        
        label = Label(parentControl, text="Adjectifs: ")
        label.grid(row = 2, column = 3)
        
        label = Label(parentControl, text="Explicite: ")
        label.grid(row = 3, column = 0)
        
        self.explicitNounsTextBox = Text(parentControl, width=15, height=20)
        self.explicitNounsTextBox.grid(row = 3, column = 0)
        
        self.explicitVerbsTextBox = Text(parentControl, width=15, height=20)
        self.explicitVerbsTextBox.grid(row = 3, column = 1)
        
        self.explicitAdjectivesTextBox = Text(parentControl, width=15, height=20)
        self.explicitAdjectivesTextBox.grid(row = 3, column = 2)
        
        label = Label(parentControl, text="Implicite: ")
        label.grid(row = 3, column = 0)
        
        self.implicitNounsTextBox = Text(parentControl, width=15, height=20)
        self.implicitNounsTextBox.grid(row = 3, column = 0)
        
        self.implicitVerbsTextBox = Text(parentControl, width=15, height=20)
        self.implicitVerbsTextBox.grid(row = 3, column = 1)
        
        self.implicitAdjectivesTextBox = Text(parentControl, width=15, height=20)
        self.implicitAdjectivesTextBox.grid(row = 3, column = 2)
        
        button = Button(parentControl, text="Sauvegarder", command=self._onSavePressed)
        button.grid(row = 4, column = 1)
    
    def setMandate(self, mandate):
        self.mandateTextBox.config(state = NORMAL)
        self.mandateTextBox.delete(1.0, END)
        self.mandateTextBox.insert(END, mandate)
        self.mandateTextBox.config(state = DISABLED)
        
    def _setTerms(self, textBox, terms):
        textBox.delete(1.0, END)
        textBox.insert(END, "\n".join(terms))
        
    def setExplicitNouns(self, nouns):
        self._setTerms(explicitNounsTextBox, nouns)
        
    def setExplicitVerbs(self, verbs):
        self._setTerms(explicitVerbsTextBox, verbs)
        
    def setExplicitAdjectives(self, adjectives):
        self._setTerms(explicitAdjectivesTextBox, adjectives)
        
    def setImplicitNouns(self, nouns):
        self._setTerms(implicitNounsTextBox, nouns)
        
    def setImplicitVerbs(self, verbs):
        self._setTerms(implicitVerbsTextBox, verbs)
        
    def setImplicitAdjectives(self, adjectives):
        self._setTerms(implicitAdjectivesTextBox, adjectives)
        
    def _getTerms(self, textBox):
        text = textBox.get(1.0, END)
        terms = whitespaceRegex.split(text)
        terms = filter(lambda x: len(x) > 0, terms)
        return 
        
    def getExplicitNouns(self):
        return self._getTerms(explicitNounsTextBox)
    
    def getExplicitVerbs(self):
        return self._getTerms(explicitVerbsTextBox)
    
    def getExplicitAdjectives(self):
        return self._getTerms(explicitAdjectivesTextBox)
    
    def getImplicitNouns(self):
        return self._getTerms(explicitNounsTextBox)
    
    def getImplicitVerbs(self):
        return self._getTerms(explicitVerbsTextBox)
    
    def getImplicitAdjectives(self):
        return self._getTerms(explicitAdjectivesTextBox)
        
    def _onSavePressed(self):
        self.listener.onTextualAnalysisChangesSaved()